<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//Require model files
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . 'Warrantydt.php';
//Require objects files
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . 'Warrantydt.php';
//Require forms files
require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . 'Warrantydt.php';

class Admin_WarrantydtController extends Zend_Controller_Action{
    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private  $modelWarrantydt; 
    private $_redirector;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////
    
    
    public function preDispatch() {

    }
    
    public function init(){
        $layoutPath = APPLICATION_PATH . '/templates/default';
        $option = array('layout' => 'index',
        'layoutPath' => $layoutPath);
        
        Zend_Layout::startMvc($option);
        $this->modelWarrantydt = new End_Models_Warrantydt();
        $this->_redirector = $this->_helper->getHelper('Redirector');
    }
    
    ///////////////////////
    // U T I L I T I E S //
    ///////////////////////
   
        
    ///////////////////
    // A C T I O N S //
    ///////////////////
    
    //INDEX ACTION
    public function indexAction(){
        $tmpArrWarrantydt = $this->modelWarrantydt->fetchAll();
        $this->view->arrWarrantydt = $tmpArrWarrantydt;
    }
    
    //ADD ACTION
    public function addAction(){
        $formWarrantydt = new Application_Form_Warrantydt();
        
        if($this->getRequest()->isGet()){
            $formWarrantydt->warrantydt_id->setValue($warrantydt_id);
        }else {
            $cancel = $this->getRequest()->getPost('cancel', '0');
            if($cancel){
                $this->_redirector->gotoUrl('admin/warrantydt/index');
            }
            
            $formData = $this->getRequest()->getPost();
            if($formWarrantydt->isValid($formData)){
                $warrantydt = new End_Models_Warrantydt();
                $warrantydt->warranty_id = '1';// gán tạm
                $warrantydt->warrantydt_date_created = date('Y-m-d');
                $warrantydt->warranty_reason = $formData['warranty_reason'];
                $warrantydt->returnedDate = $formData['returnedDate'];
                $warrantydt->description = $formData['description'];
                
                $this->modelWarrantydt->_insert($warrantydt);
                $this->_redirector->gotoUrl('admin/warrantydt/index');
            }  else {
                $formWarrantydt->populate($formData);
            }
        }
        
        $this->view->form = $formWarrantydt;
    }
    
    //EDIT ACTION
    public function editAction(){
        $formWarrantydt = new Application_Form_Warrantydt();
        $id = $this->getRequest()->getParam('id', '0');
        $warrantydt = $this->modelWarrantydt->_find($id);
        
        if(isset ($warrantydt)){
            if($this->getRequest()->isPost()){
                $formWarrantydt->warrantydt_id->setValue($warrantydt_id);
                $cancel = $this->getRequest()->getPost('cancel', '0');
                if($cancel){
                    $this->_redirector->gotoUrl('admin/warrantydt/index');
                }
                
                $formData = $this->getRequest()->getPost();
                
                if($formWarrantydt->isValid($formData)){
                    //$warrantydt = new End_Models_Warrantydt();
                    $warrantydt->warranty_id = $formData['warranty_id'];// gán tạm
                    $warrantydt->warrantydt_date_created = date('Y-m-d');
                    $warrantydt->warranty_reason = $formData['warranty_reason'];
                    $warrantydt->returnedDate = $formData['returnedDate'];
                    $warrantydt->description = $formData['description'];

                    $id = $this->modelWarrantydt->_update($warrantydt);
                    $this->_redirector->gotoUrl('admin/warrantydt/index');
                }  else {
                    $formWarrantydt->populate($formData);
                    $this->view->form = $formWarrantydt;
                }
            }  else {
                $UC = new Utility_Converter();
                $formWarrantydt->populate($UC->objectToArray($warrantydt));
                $this->view->form = $formWarrantydt;
            }
        }  else {
            $this->_redirector->gotoUrl('admin/warrantydt/index');
        }
    }
    
    //DELETE ACTION
    public function deleteAction(){
        $id = $this->getRequest()->getParam('id', '0');
        $warrantydt = $this->modelWarrantydt->_find($id);
        if(isset ($warrantydt)){
            $this->modelWarrantydt->_delete($id);
        }
        $this->_redirector->gotoUrl('admin/warrantydt/index');
    }
    
}
?>
